package com.it.data_recursion.binarySearch;

/**
 * @Author: CaoYouGen
 * @DateTime: 2023/05/19/11:26
 * @注释: 递归（二分查找）
 **/
public class RecursionBinarySearch {
    public static int getRBS(int[] arr, int data){
        return toGetRS(arr, data, 0, arr.length - 1);
    }

    private static int toGetRS(int[] arr, int data, int left, int right){
        int index = (left + right) >>> 1;
        int i = arr[index];
        if (left > right){
            return -1;
        }
        if (i == data){
            return index;
        }else if (data < i){
            return toGetRS(arr, data,left, i - 1);
        }else if (data > i){
            return toGetRS(arr, data,i + 1, right);
        }
        return -1;
    }
}
